﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Senparc.Weixin.QY;
using Senparc.Weixin.QY.AdvancedAPIs;
using Senparc.Weixin.QY.AdvancedAPIs.MailList;

namespace Common.UserApi
{
    public class User
    {

        #region 获取成员列表
        /// <summary>
        /// 获取成员列表
        /// </summary>
        /// <param name="department_id">部门id</param>
        /// <param name="status">0获取全部员工，1获取已关注成员列表，2获取禁用成员列表，4获取未关注成员列表。status可叠加</param>
        public static List<GetMemberResult> getAllUser(int department_id, int status=0)
        {
            var userList = MailListApi.GetDepartmentMemberInfo(accesstoken(), department_id, 1, status);
            return userList.userlist;
        }
        #endregion

        #region 获取单人信息
        /// <summary>
        /// 获取单人信息
        /// </summary>
        /// <param name="Userid">用户Userid</param>
        public static GetMemberResult userInfo(string Userid)
        {
            var user = MailListApi.GetMember(accesstoken(), Userid);
            return user; 
        }
        #endregion

        #region UserId和OpenId互转
        /// <summary>
        /// UserId和OpenId互转
        /// </summary>
        /// <param name="UorO">userid或者openid</param>
        /// <param name="type">类型0为userid,其他为openid</param>
        /// <returns></returns>
        public static string UserOrOenid(string UorO,int type=0)
        {
            var str = "";
            if (type == 0)
            {
                var result = Senparc.Weixin.QY.CommonAPIs.CommonApi.ConvertToOpenId(accesstoken(), UorO);
                str = result.openid;
            }
            else
            {
                var result = Senparc.Weixin.QY.CommonAPIs.CommonApi.ConvertToUserId(accesstoken(), UorO);
                str = result.userid;
            }
            return str;
        }
        #endregion

        #region  获取accesstoken
        /// <summary>
        ///  获取accesstoken超时时间为7200毫秒 可以放进数据库或者缓存里面  微信限制调用次数
        /// </summary>
        /// <returns></returns>
        public static string accesstoken()
        {
            return Senparc.Weixin.QY.Containers.AccessTokenContainer.TryGetToken(Config.CorpID, Config.Corpsecret);
        }
        #endregion
    }
}
